package img 
{
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import ru.gosoplata.model.TmpModel;
	
	/**
	 * TODO: переписать вью на перья
	 * даже не буду переписывать! - СУКА! ПРИШЛОСЬ!
	 */
	public class ScalePlane extends Sprite 
	{
		public static var ON_PLANE_CLICK:String = "ON_PLANE_CLICK";
		
		private var pwd:Number;
		private var iwd:Number;
		private var in_x:Number;
		private var fields_count:Number;
		public var prefix_txt:TextField;
		
		private var pl_left:Bitmap =new Bitmap();
		private var pl_right:Bitmap=new Bitmap();
		private var pl_center:Bitmap=new Bitmap();
		private var red_left:Bitmap =new Bitmap();
		private var red_right:Bitmap=new Bitmap();
		private var red_center:Bitmap=new Bitmap();
		private var in_left:Bitmap=new Bitmap();
		
		private var swPhone:Boolean;
		
		private var  input_field1:InputPlane;
		private var  input_field2:InputPlane;
		
		private var _isready:Boolean;
		public var digitlimit:Number = 0;
		public var tab_index:Number;
		
		public var swReady:Boolean = false;
		
		public function ScalePlane(plane_wd:Number, input_wd:Number = 0, input_x:Number = 3, fields_cnt:Number = 1, is_phone:Boolean = false ) 
		{
			swPhone = is_phone;
			fields_count = fields_cnt;
			pwd = plane_wd;
			iwd = input_wd;
			if (iwd == 0) iwd = pwd - 6;
			in_x = input_x;
			createPlane();
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		protected function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(MouseEvent.CLICK, onClick);
		}
		
		protected function onClick(e:MouseEvent):void 
		{
			dispatchEvent(new Event(ON_PLANE_CLICK,true));
		}
		
		public function getText():String {
			return input_field1.getText();
		}
		
		public function curr_plane_wd():Number {
			return pwd;
		}
		
		public function curr_input_wd():Number {
			return iwd;
		}
		
		public function set text(txt:String):void {
			input_field1.text = txt;
		}
		
		public function set prefix(txt:String):void {
			prefix_txt.text = txt;
			prefix_txt.visible = (prefix_txt.text.length > 0);
		}
		
		public function get isready():Boolean 
		{
			return _isready;
		}
		
		public function set isready(value:Boolean):void 
		{
			_isready = value;
		}
		
		public function reScale(plane_wd:Number, input_wd:Number = 0,input_x:Number = 3):void {
			pwd = plane_wd;
			iwd = input_wd;
			if (iwd == 0) iwd = pwd - 6;
			in_x = input_x;

			pl_center.width = pwd - pl_left.width - pl_right.width;
			red_center.width = pl_center.width+1;
			
			pl_left.x = 0;
			pl_center.x = pl_left.x + pl_left.width;
			pl_right.x = pl_center.x + pl_center.width;
			red_center.x = pl_center.x-1;
			red_right.x = pl_right.x;
			
			input_field1.x = in_x;
			input_field1.reScale(iwd);

			prefix_txt.x = pl_center.x;
			prefix_txt.width = input_field1.x - pl_center.x;
			prefix_txt.visible = (prefix_txt.text.length > 0);
			this.cacheAsBitmap = true;
		}
		
		private function createPlane():void 
		{
			pl_left.bitmapData = TmpModel.loadedImages.getBitmapDataByName("plane_left").bitmapData;
			pl_right.bitmapData = TmpModel.loadedImages.getBitmapDataByName("plane_right").bitmapData;
			pl_center.bitmapData = TmpModel.loadedImages.getBitmapDataByName("plane_center").bitmapData;
			pl_left.smoothing = true;
			pl_right.smoothing = true;
			pl_center.smoothing = true;
			red_right.bitmapData = TmpModel.loadedImages.getBitmapDataByName("plane_chek_right").bitmapData;
			red_left.bitmapData = TmpModel.loadedImages.getBitmapDataByName("plane_chek_left").bitmapData;
			red_center.bitmapData = TmpModel.loadedImages.getBitmapDataByName("plane_chek_center").bitmapData;
			red_center.smoothing = true;
			red_left.smoothing = true;
			red_right.smoothing = true;
			red_center.visible = false;
			red_left.visible = false;
			red_right.visible = false;
			
			pl_center.width = pwd - pl_left.width - pl_right.width;
			red_center.width = pwd - red_left.width - red_right.width+1;// pl_center.width;
			pl_left.x = 0;
			addChild(pl_left);
			red_left.x = pl_left.x;
			addChild(red_left);
			pl_center.x = pl_left.x + pl_left.width;
			addChild(pl_center);
			red_center.x = pl_center.x-1;
			addChild(red_center);
			pl_right.x = pl_center.x + pl_center.width;
			addChild(pl_right);
			red_right.x = pl_right.x;
			addChild(red_right);
			
			if(!swPhone){
				input_field1 = new InputPlane(iwd);
				input_field1.x = in_x;
				addChild(input_field1);
			}else {
				input_field1 = new InputPlane(130);
				input_field1.x = in_x;
				addChild(input_field1);
				
				input_field2 = new InputPlane(pwd - in_x - input_field1.width - 20);
				input_field2.x = input_field1.x + input_field1.width+10;
				addChild(input_field2);
			}
			
			var txt_type2:TextFormat = TmpModel.getTextFormat(52, 0xFFFFFF);
			txt_type2.letterSpacing = 7;
			
			prefix_txt = new TextField();
			prefix_txt.embedFonts = true;
			prefix_txt.defaultTextFormat = txt_type2;
			prefix_txt.y = pl_center.height / 2 - prefix_txt.height / 3;//input_txt.y;
			prefix_txt.x = pl_center.x;
			prefix_txt.width = in_left.x - pl_center.x;
			prefix_txt.thickness = 100;
			prefix_txt.antiAliasType = AntiAliasType.ADVANCED;
			prefix_txt.selectable = false;
			prefix_txt.visible = (prefix_txt.text.length > 0);
			
			addChild(prefix_txt);
			
			isCheck(true);
		}
		
		public function setMaskOnField(masknum:Number ):void {
			input_field1.txtmask = masknum;
		}
		
		public function getMask():Number {
			return input_field1.txtmask;
		}
		public function isCheck(sw:Boolean):void {
			red_center.visible = sw;
			red_left.visible = sw;
			red_right.visible = sw;
			pl_center.visible = !sw;
			pl_left.visible = !sw;
			pl_right.visible = !sw;
			input_field1.isCheck(sw);
		}
	}

}